extends Control

@export var max_count_per_page: int = 10
var current_page_index: int = 0
var total_page_count: int = 1
var items_data: Array[ItemData]
@onready var item_container: Control = $ItemContainer
@export var radius: float = 60
@onready var bg: ColorRect = $Bg

var is_open: bool = false
var mouse_start_position: Vector2 = Vector2.ZERO
@export var select_dead_zone_radius: float = 10
@export var select_max_radius: float = 20 
var current_selected_item_node: Control = null

@onready var arrow_left: Control = $MarginContainer/Control/ArrowLeft
@onready var arrow_right: Control = $MarginContainer/Control/ArrowRight

# level
var level: Level

# mouse position recover
var game_mouse_position: Vector2

# item description
@onready var item_desc: Label = $ItemDesc

signal signal_on_close


func update_arrow_view():
	if current_page_index > 0:
		arrow_left.visible = true
	else:
		arrow_left.visible = false
	
	if total_page_count > current_page_index + 1:
		arrow_right.visible = true
	else:
		arrow_right.visible = false


func open(items_data: Array[ItemData]):
	if items_data.size() <= 0: return
	
	game_mouse_position = get_viewport().get_mouse_position()
	
	self.items_data = items_data 
	total_page_count = ceil(self.items_data.size() * 1.0 / max_count_per_page) 
	current_page_index = 0
	update_arrow_view()
	
	show_current_page()
	
	modulate.a = 0
	var tween = create_tween()
	tween.tween_property(self, "modulate:a", 1.0, 0.2) \
		.set_trans(Tween.TRANS_SINE) \
		.set_ease(Tween.EASE_OUT)
	
	Input.mouse_mode = Input.MOUSE_MODE_HIDDEN
	GlobalUtils.set_mouse_center()
	mouse_start_position = get_viewport().get_mouse_position()
	is_open = true
	

func show_current_page():
	destroy_all_item()
	
	var current_page_start_index = current_page_index * max_count_per_page
	var current_page_end_index = current_page_start_index + max_count_per_page
	if current_page_end_index >= items_data.size(): current_page_end_index = items_data.size()
	var current_page_size = current_page_end_index - current_page_start_index
	
	var angle_deg_increment = 360.0 / current_page_size
	var angle_deg = 0.0
	var center = get_viewport().get_visible_rect().size / 2.0
		
	for i in range(current_page_start_index, current_page_end_index):
		var item_data = items_data[i]
		var item_inst = create_new_item(item_data)
		item_container.add_child(item_inst)
		
		var angle_rad = deg_to_rad(angle_deg) # Godot 用弧度计算
		var pos = center + Vector2(cos(angle_rad), sin(angle_rad)) * radius
		item_inst.position = pos
		
		angle_deg += angle_deg_increment
		
		
func create_new_item(item_data: ItemData):
	var item_inst = ResourceManager.get_resource("quick_switch_item").instantiate()
	item_inst.init(item_data)
	return item_inst


func destroy_all_item():
	for i in range(item_container.get_child_count()):
		item_container.get_child(i).queue_free()
		

func close():
	is_open = false
	signal_on_close.emit()
	GlobalUtils.set_mouse_position(game_mouse_position)
	queue_free()
	
	if current_selected_item_node:
		GlobalSignals.signal_switch_hold_item.emit(current_selected_item_node.item_data)


func _process(delta: float) -> void:
	process_select(delta)
	process_page_change()
	
	
func process_select(delta):
	if not is_open: return
	
	var now_mouse_position = get_viewport().get_mouse_position()
	var dir = (now_mouse_position - mouse_start_position).normalized()
	var dist = (now_mouse_position - mouse_start_position).length()
	
	if dist > select_max_radius:
		dist = select_max_radius
		get_viewport().warp_mouse(mouse_start_position + dist * dir)
		now_mouse_position = get_viewport().get_mouse_position()
	
	if dist > select_dead_zone_radius:
		# select one
		var angle = atan2(dir.y, dir.x)
		var angle_deg = fposmod(rad_to_deg(angle), 360.0)
		var slot_angle = 360.0 / item_container.get_child_count()
		var index = int(floor((angle_deg + slot_angle/2) / slot_angle)) % item_container.get_child_count()
		
		var target_node = item_container.get_child(index)
		if target_node != current_selected_item_node:
			if current_selected_item_node:
				current_selected_item_node.unselect()
				current_selected_item_node = null
				
			current_selected_item_node = target_node
			current_selected_item_node.select()
			
			item_desc.text = current_selected_item_node.item_data.item_config.name + "\n" + current_selected_item_node.item_data.item_config.item_desc
	else:
		if current_selected_item_node:
			current_selected_item_node.unselect()
			current_selected_item_node = null
			
			item_desc.text = ""
	
	
func process_page_change():
	if not is_open: return
	
	var next_page_index = current_page_index
	
	if Input.is_action_just_pressed("page_left"):
		next_page_index -= 1
		if next_page_index < 0: next_page_index = 0
	
	if Input.is_action_just_pressed("page_right"):
		next_page_index += 1
		if next_page_index >= total_page_count:
			next_page_index -= 1
			
	if next_page_index != current_page_index:
		current_page_index = next_page_index
		show_current_page()
		update_arrow_view()
	
	
	
	


		
